package alipay.openplatform.edu.campus.jiuye.service.services.system;


import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.system.SchoolInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.SchoolInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.SchoolInfoVO;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.system.ISchoolInfoService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("SchoolInfoService")
public class SchoolInfoService implements ISchoolInfoService {


    //日志操作类
    private static Logger logger = LoggerFactory.getLogger(AreaInfoService.class);


    //地区DAO对象
    @Autowired
    SchoolInfoMapper schoolInfoMapper;


    /**
     * 分页获取学校信息 - ZONG
     *
     * @param schoolName 城市名称
     * @param pageNo     当前页的编号
     * @param pageSize   每页的行数
     * @return
     */
    public ApiResult getSchoolPage(@Param("schoolName") String schoolName, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize) throws Exception {

       // schoolName = new String(schoolName.getBytes("iso-8859-1"), "utf-8");

        logger.info("根据查询条件分页获取申请单的信息|SchoolInfoService|getSchoolPage|params:cityName={},pageNo={},pageSize={}", schoolName, pageNo, pageSize);

        //1. 获取总页数
        int count = schoolInfoMapper.getSchoolInfoCount(schoolName);

        //2. 获取当前开始的行的位置
        int startPages = (pageNo - 1) * pageSize;
        if (startPages < 0) startPages = 0;

        //3. 分页获取数据
        List<SchoolInfoPO> list = schoolInfoMapper.getSchoolPage(schoolName, startPages, pageSize);

        List<SchoolInfoVO> listVO = CopyVoPo.copyList(list, SchoolInfoVO.class);
        //4. 返回分页数据
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total_num", count);
        result.put("list", listVO);

        return ApiResult.buildSuccess("0", "分页返回数据成功!", result);

    }


    /**
     * 删除学校信息 - ZONG
     *
     * @param schoolId 城市ID
     * @return
     */
    public ApiResult delSchool(String schoolId) {
        logger.info("根据查询条件分页获取申请单的信息|SchoolInfoService|delSchool|params:schoolId={}", schoolId);

        int count = schoolInfoMapper.deleteByPrimaryKey(schoolId);
        if (count > 0) {
            return ApiResult.buildSuccess("0", "删除数据成功!", null);

        }
        return ApiResult.buildSuccess("-1", "没有数据可以删除!", null);

    }

    /**
     * 通过学校编码获取城市编码
     *
     * @param schoolId
     * @return
     */
    public Long getCityIdBySchoolId(String schoolId) {
        logger.info("根据学校编码获取城市编码|SchoolInfoService|getCityIdBySchoolId|params:schoolId={}", schoolId);
        return schoolInfoMapper.getCityIdBySchoolId(schoolId);
    }
}